//
//
//  Created by 小柠檬🍋 on 2025/9/7.
//

import SwiftUI

//错误日志
struct AiCustomErrorView: View {
    var bean : AiCustomBean
    @StateObject private var viewModel = AiCustomErrorViewModel()
    @State private var choose : Bool = false
    
    var body: some View {
        ZStack{
            if !viewModel.status.isEmpty {
                HomeEmptyView(text1: viewModel.status)
                    .frame(maxWidth: .infinity, maxHeight: .infinity)
            } else {
                VStack {
                    ScrollView {
                        Spacer(minLength: 10)
                        //需要嵌套一层LazyVStack，不然会产生间距，而且大小还会随机
                        LazyVStack(spacing: 15) {
                            ForEach(viewModel.items,id: \.objectId) { item in
                                ApiCustomErrorItemView(bean: item,choose: $choose)
                            }
                            LoadMoreFooter(refreshing: $viewModel.footerRefreshing, action: {
                                viewModel.getData(false, id: bean.objectId ?? "")
                            }) {
                                LoadMoreView(noMore: viewModel.noMore, footerRefreshing: viewModel.footerRefreshing, hideNoMore: true)
                            }
                            .noMore(viewModel.noMore)
                            .preload(offset: 10)
                        }
                    }
                    .enableRefresh()
                    .scrollIndicators(.hidden) // 隐藏滚动条
                    .padding(.leading, 20)
                    .padding(.trailing,15)
                    if choose{
                        Text("删除")
                            .font(.system(size: 14))
                            .foregroundColor(.white)
                            .frame(maxWidth: .infinity)
                            .frame(height: 50)
                            .background(.bgRed)
                            .contentShape(Rectangle())
                            .onTapGesture {
                                let select = viewModel.items.filter{($0.select ?? false) == true}
                                viewModel.doDel(list: select) {
                                    choose = false
                                    viewModel.getData(true, id: bean.objectId ?? "")
                                }
                            }
                    }
                }
            }}
        .navigationTitle("自定义API")
        .ignoresSafeArea(edges: .bottom)
        .frame(maxWidth: .infinity)
        .background(.bg)
        .toolbar {
            // 添加一个右标题按钮
            ToolbarItem(placement: .navigationBarTrailing) {
                Text(choose ? "取消选择" : "选择")
                    .foregroundColor(themeManager.themeColor)
                    .font(.system(size: 12))
                    .padding(.horizontal,10)
                    .padding(.vertical,7)
                    .cornerRadius(8)
                    .contentShape(Rectangle())
                    .onTapGesture {
                        choose = !choose
                    }
            }
        }
        .onAppear{
            if !viewModel.isInit {
                viewModel.getData(true, id: bean.objectId ?? "")
                viewModel.isInit = true
            }
        }
    }
}

//#Preview {
//    ChatListView()
//}

//定义预览提供者
struct AiCustomErrorView_Previews: PreviewProvider {
    static var previews: some View {
        //使用预览共享配置
        configurePreviewEnvironment(for: AiCustomErrorView(bean: AiCustomBean()))
    }
}
